﻿#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Erp 
// 
// Project : Jackie.Utility.BvConversion
// 
// File : DateTimeStringValueConversion.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : DateTime string 转换
// 
// History : 
//			2012-05-15 11:42:20	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using Jackie.Utility.CustomExtension;

#endregion

namespace Jackie.Utility.BvConversion.DefalutValueConversion
{
    ///<summary>
    ///DateTime string 转换
    ///</summary>
    public class DateTimeStringValueConversion : IValueConversion
    {
        #region Implementation of IValueConversion<DateTime,string>

        public object GetViewValue(object businessValue)
        {
            var dateValue = (DateTime)businessValue;
            if (dateValue == DateTimeExtension.MinDate)
                return string.Empty;

            if (dateValue.Hour == 0 && dateValue.Minute == 0 && dateValue.Second == 0)
                return dateValue.ToString("yyyy-MM-dd");

            return dateValue.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public object GetBusinessValue(object viewValue)
        {
            if (viewValue == null || viewValue.ToString() == "")
                return DateTimeExtension.MinDate;

            var date = JackieConvertor.ToDataTimeThrowException(viewValue.ToString());
            if (date == DateTime.MinValue)
                return DateTimeExtension.MinDate;
            return date;
        }

        #endregion
    }
}